/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @author: jin Chen 
 * @date: Jul 7, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.plot.control;

import edu.psu.geovista.app.pcp.plot.control.tools.*;
import edu.psu.geovista.app.pcp.plot.gui.BasicPcpUI;

import javax.swing.*;
import javax.swing.event.MouseInputListener;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;

public class ToolManager {
    public static final boolean DEBUG=false;

    private Hashtable cmds=new Hashtable();

    BasicPcpUI ui;
    private JFileChooser fc;

    /**
     * Listen to mouse/ mouse motion event
     * Candidate: DataSelectCmd, AxisOrderCmd,AxisPlaceCmd
     */
    private MouseInputListener defaultMouseListener;
    /**
     * Creates default action controller.
     *
     * @param ui The instance of the plot's UI.
     */
    public ToolManager(BasicPcpUI ui) {
         this.ui = ui;
         if(ui.getPlot().getDataModel() !=null)
            this.setupCmds(ui);
         //this.defaultMouseListener =(MouseInputListener) cmds.get(BasicCmd.AxisOrderT ) ; not set here, set in toolbar
    }
    /*******************************************************************************************************
     *                Tools: a tool direct various layers to complete a task
     *******************************************************************************************************/
     public void setupCmds(BasicPcpUI ui){
        //cmds=new Hashtable();
         //AxisOrderTool here ???
        //AxisScaleCmd ast=new AxisScaleCmd(ui );
        //cmds.put(BasicCmd.AxisScaleT,ast);
        //cmds.clear();

        AxisShowRangeTool asrt=new AxisShowRangeTool(ui);
        cmds.put(BasicTool.AxisShowRangeT,asrt);

        AxisOrderTool aot=new AxisOrderTool(ui);
        cmds.put(BasicTool.AxisOrderT ,aot);

        AxisMoveTool amvc=new AxisMoveTool(ui);
        cmds.put(BasicTool.AxisMoveT ,amvc);

        AxisPlaceTool aplc=new AxisPlaceTool(ui);
        cmds.put(BasicTool.AxisPlaceT ,aplc);
        edu.psu.geovista.app.plot.axis.XAxisOrder xaxis = ui.getPlot().getXaxis();
        xaxis.addPropertyChangeListener(aplc);

        DataSelectTool dbt=new DataSelectTool(ui);
        cmds.put(BasicTool.DataSelectT ,dbt);

        //DataSetColorCmd dsct=new DataSetColorCmd(ui);
        //cmds.put(BasicCmd.DataSetColorT ,dsct);

        PlotTool plotTool=new PlotTool(ui);
        cmds.put(BasicTool.PlotT,plotTool);

        AnimationTool anmCmd=new AnimationTool(ui);
        cmds.put(BasicTool.AnmT,anmCmd);

        DefaultMouseTool dmcmd=new DefaultMouseTool(ui);
        cmds.put(BasicTool.DMouseT ,dmcmd);

      

    }

    /**
     * reset all tools to initial state without recreating them.
     *  
     *
     */
    public void disposeTools(){
        //make each tool disposed
        Collection toolList = cmds.values();
        for (Iterator iterator = toolList.iterator(); iterator.hasNext();) {
            BasicTool tool = (BasicTool) iterator.next();
            tool.dispose();

        }

        //clear them
        cmds.clear();


    }

    /**
     * Callback method called when the active controller is about to be deactivated.
     */
    public void deactivated() {
        // do nothing
    }



    /**
     * Invoked when an action occurs.
     * See actions' name in ActionManager
     *
     * @param actionName The name of the action to process.
     */
    /*public boolean processAction(String actionName) {
        //System.out.println("Controller process "+actionName);
        if (actionName.equals("StartAnm") ){
           AnimationCmd cmd=(AnimationCmd) cmds.get(BasicCmd.AnmCmd );
           if(cmd!=null)
            cmd.start() ;
        }
        else if (actionName.equals("StepAnm") ){
           AnimationCmd cmd=(AnimationCmd) cmds.get(BasicCmd.AnmCmd );
           if(cmd!=null)
           cmd.step() ;
        }
        else if (actionName.equals("PauseAnm") )   {
           AnimationCmd cmd=(AnimationCmd) cmds.get(BasicCmd.AnmCmd );
           if(cmd!=null)
           cmd.pause() ;
        }
        else if (actionName.equals("StopAnm") ){
           AnimationCmd cmd=(AnimationCmd) cmds.get(BasicCmd.AnmCmd );
           if(cmd!=null)
           cmd.stop() ;
        }
        else if(actionName.equals("Select")){
            DataSelectCmd scmd=(DataSelectCmd)cmds.get(BasicCmd.DataSelectT );

            if(scmd!=null) {
                MouseObserver mob = this.ui.getMob();
                mob.setListener(scmd);
            }
            else{
                //System.out.println("Fail to set Select button");
            }

        }
        else if(actionName.equals(ActionManager.ACTION_HIGHLIGHT)){
            DataSelectCmd scmd=(DataSelectCmd)cmds.get(BasicCmd.DataSelectT );

            if(scmd!=null) {
                scmd.setHightLightMode(!scmd.isHightLightMode() );

            }

        }
        else if(actionName.equals("OrderAxis")){
            AxisOrderCmd axOrdCmd=(AxisOrderCmd)cmds.get(BasicCmd.AxisOrderT );
            this.ui.getMob().setListener(axOrdCmd);

        }
        else if(actionName.equals("MoveAxis")){
            AxisMoveCmd axMvCmd=(AxisMoveCmd)cmds.get(BasicCmd.AxisMoveCmd );
            this.ui.getMob().setListener(axMvCmd);

        }
        else if (actionName.equals("UpDownAxis")){
            AxisShowRangeCmd ashCmd=(AxisShowRangeCmd)cmds.get(BasicCmd.AxisShowRangeT );
            this.ui.getMob().setListener(ashCmd);
        }
        else if (actionName.equals("DataShowPart")){
            DataSelectCmd dsc=(DataSelectCmd) cmds.get(BasicCmd.DataSelectT ) ;
            dsc.reverseShowRangedData() ;
        }
        else if(actionName.equals("DataShowInvalid")){
            //System.out.println("DataShowInvalid");
            DataSelectCmd dsc=(DataSelectCmd) cmds.get(BasicCmd.DataSelectT ) ;
            if(dsc!=null)
            dsc.reverseShowInvalidData() ;

        }
        else if (actionName.equals(ActionManager.ACTION_DATASHOWALL)){
            DataSelectCmd dsc=(DataSelectCmd) cmds.get(BasicCmd.DataSelectT ) ;
            if(dsc!=null)
            dsc.updateShowBackgroundData();
        }
        else if (actionName.equals("ShowBoxplot")){
             AxisShowRangeCmd axShowCmd=(AxisShowRangeCmd) cmds.get(BasicCmd.AxisShowRangeT );

            if (axShowCmd!=null) {
                axShowCmd.reverseShowBoxPlot() ;
            }
        }
        else if (actionName.equals("AlignBoxplot")){
             AxisShowRangeCmd axShowCmd=(AxisShowRangeCmd) cmds.get(BasicCmd.AxisShowRangeT );
             if(axShowCmd!=null)
             axShowCmd.alignBoxPlot() ;
        }
        else if (actionName.equals("Reset")){
             AxisShowRangeCmd axShowCmd=(AxisShowRangeCmd) cmds.get(BasicCmd.AxisShowRangeT );
             if(axShowCmd!=null)
             axShowCmd.resetRanges() ;
        }
        else if (actionName.equals("OpenFile")){
             // getDataFromFile();
            showLoaderDialog();
        }
        else if (actionName.equals("FitWindow")){


            AxisPlaceCmd apCmd=(AxisPlaceCmd) this.getCmd(BasicCmd.AxisPlaceCmd );
            if(apCmd!=null) apCmd.fitwindow();


        }
        else if(actionName.equals("ShowMedianSummary")){
            DataSelectCmd dsc=(DataSelectCmd) cmds.get(BasicCmd.DataSelectT );
            if(dsc!=null){
            dsc.setMode(DataSelectCmd.MODE_CategorySummary ) ;
            dsc.showData();
            }
        }
        else if(actionName.equals("ShowObSummary")){

            DataSelectCmd dsc=(DataSelectCmd) cmds.get(BasicCmd.DataSelectT );
            if(dsc!=null){
            dsc.setMode(DataSelectCmd.MODE_ObservationSummary ) ;
            dsc.showData();
            }
        }
        else if(actionName.equals("NormalMode")){
            DataSelectCmd dsc=(DataSelectCmd) cmds.get(BasicCmd.DataSelectT );
            if(dsc!=null){
            dsc.setMode(DataSelectCmd.MODE_Normal ) ;
            dsc.showData() ;
            //System.out.println("NormalMode");
            }
        }
        else if(actionName.equals("ShowCorrelation")){
           AxisShowRangeCmd cmd=(AxisShowRangeCmd) cmds.get(BasicCmd.AxisShowRangeT);
           cmd.showCorrelation(); 
        }
        else if(actionName.equals("FrameScan") ){
            System.out.println("FrameScan");
            ScanBoxTool fst = (ScanBoxTool) cmds.get(BasicCmd.ScanBoxTool );
            fst.showScanBox();
        }
        return true;
    }*/
    /*******************************************************************************************************
     *                tools that not work on plot
     *******************************************************************************************************/
 /*   private void showLoaderDialog(){

        DataLoaders dl=new StandAlonePcpDataLoader();
        Pcp pcp=this.ui.getPlot();
        dl.addDataSetListener(pcp);
        dl.addTsDataListener(pcp);
        dl.startWizard() ;

    }*/
   /* private void getDataFromFile() {

        Pcp pcp=this.ui.getPlot() ;
        String appRoot=pcp.getAppRoot() ;
        String dataRoot=appRoot+"data";
        if(fc==null)
            fc=new JFileChooser(dataRoot);//"c:/pcphome/data/NCI");
        int act=fc.showOpenDialog(pcp);
        File file=null;

        if (act==JFileChooser.APPROVE_OPTION ){
           file=fc.getSelectedFile() ;
        }
        if (file!=null){
            String fullName=null;
            try {
                 fullName=file.getCanonicalPath() ;
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use Options | File Templates.
            }
            pcp.extractDataFromFile(fullName);
            fc.setCurrentDirectory(file.getParentFile().getParentFile() );
        }

    }*/
   /* public int getActionState(String actionName) {
        return ActionManager.ACTION_ENABLED;
    }
*/
    public BasicTool getCmd(String name) {

        BasicTool tool = (BasicTool)this.cmds.get(name);
        //todo
        if(tool==null){
            if(DEBUG) {
                System.out.println("cmds:"+Integer.toHexString(cmds.hashCode() ));
                System.out.println("null cmds");
            }
        }
        else{
            if(DEBUG)
            System.out.println("cmds:"+Integer.toHexString(cmds.hashCode() ));
        }

        return tool ;
    }
    public Hashtable getCmds() {
        return this.cmds;
    }

    public MouseInputListener getDefaultMouseListener() {
        return defaultMouseListener;
    }

    public BasicPcpUI getUi() {
        return ui;
    }

}
